package com.cml.sqlversionupgradestarter.context;

import cn.hutool.core.date.DateUtil;
import com.cml.sqlversionupgradestarter.service.JdbcConnManager;
import com.cml.sqlversionupgradestarter.service.SqlVersionCTLManager;

import javax.sql.DataSource;
import java.util.Date;

/**
 * 姓名：陈茂林
 * 时间：2023/10/24 16:40
 * 描述：
 */
public class SQLVersionUpgradeContext {
    private SqlVersionCTLManager sqlVersionContextService;
    private SqlUpgradeMetadataContext sqlUpgradeContext;
    private Date dbUpgradeTime;

    private JdbcConnManager jdbcConnManager;

    public JdbcConnManager getJdbcConnManager() {
        return jdbcConnManager;
    }

    public void setJdbcConnManager(JdbcConnManager jdbcConnManager) {
        this.jdbcConnManager = jdbcConnManager;
    }

    private SQLVersionUpgradeContext() {
    }

    public SqlVersionCTLManager getSqlVersionContextService() {
        return sqlVersionContextService;
    }


    public SqlUpgradeMetadataContext getSqlUpgradeContext() {
        return sqlUpgradeContext;
    }

    public void setSqlUpgradeContext(SqlUpgradeMetadataContext sqlUpgradeContext) {
        this.sqlUpgradeContext = sqlUpgradeContext;
    }

    public Date getDbUpgradeTime() {
        return dbUpgradeTime;
    }

    public void setDbUpgradeTime(Date dbUpgradeTime) {
        this.dbUpgradeTime = dbUpgradeTime;
    }

    public static class Builder{

        private Date dbUpgradeTime;

        private  JdbcConnManager jdbcConnManager;

        public Builder createJdbcConnManager(DataSource dataSource){
            this.jdbcConnManager = new JdbcConnManager();
            this.jdbcConnManager.setDataSource(dataSource);
            return this;
        }

        public Builder createSqlVersionUpgradeService(String dbUpgradeTime){
            this.dbUpgradeTime= DateUtil.parse(dbUpgradeTime, "yyyyMMdd");;
            return this;
        }



        public SQLVersionUpgradeContext build(){
            SQLVersionUpgradeContext sqlVersionUpgradeContext = new SQLVersionUpgradeContext();
            sqlVersionUpgradeContext.setDbUpgradeTime(this.dbUpgradeTime);
            sqlVersionUpgradeContext.setSqlUpgradeContext(new SqlUpgradeMetadataContext());
            sqlVersionUpgradeContext.setJdbcConnManager(this.jdbcConnManager);
            return sqlVersionUpgradeContext;
        }
    }


}
